<?PHP
include("settings.php");
include("connect.php");
include("functions.php");
limits();
include("queries.php");
include("header.php");

if (isset($_GET['typeID']) && !empty($_GET['typeID'])) {
$row_all = $db->fetch_assoc($db->query($query18));
$row_month = $db->fetch_assoc($db->query($query19));
$row_week = $db->fetch_assoc($db->query($query20));
}

echo '<div id="header">
<ul id="tabnav">
	<li><a href="index.php">Home</a></li>
	'.($settings['personaljournal'] == true ? "<li><a href=\"journal.php\">Personal Journal</a></li>" : "").'
	'.($settings['personaltransactions'] == true ? "<li><a href=\"transactions.php\"".($_GET['wallet'] == 'wallet' ? " class=\"active\"" : "").">Personal Transactions</a></li>" : "").'
	'.($settings['corpjournal'] == true ? "<li><a href=\"corpj.php\">Corporate Journal</a></li>" : "").'
	'.($settings['corptransactions'] == true ? "<li><a href=\"corpt.php\"".($_GET['wallet'] == 'wcorp' ? " class=\"active\"" : "").">Corporate Transactions</a></li>" : "").'
	<li><a href="overview.php?&wallet='.mysql_real_escape_string($_GET['wallet']).'" class="active">Overview</a></li>
	<li id="time">'.get_current_time().'</li>
</ul>
</div>';

echo '
<table id="wrapper" border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td>';

if (isset($_GET['typeID']) && !empty($_GET['typeID'])) {
echo '	
	<div style="float:left;width:400px;">
		
<div style="float:left">
<img src="item.php?i='.intval($_GET['typeID']).'" alt="'.get_typeName($_GET['typeID'],type).'" title="'.get_typeName($_GET['typeID'],type).'" width="64" height="64" border="0" align="middle" /> '.get_typeName($_GET['typeID'],type).'
</div>
<div style="clear:right;clear:left;">
		<table style="text-align:right;width:400px;">
		<tr>
		<td style="width:75px;"></td>
		<td>Average</td>
		<td>Minium</td>
		<td>Maximum</td>
		<td>Volume</td>
		</tr>
		<tr>
		<td>All Time</td>
		<td>'.number_format(@$row_all[price]/$row_all[quantity],'2','.',',').'</td>
		<td>'.number_format($row_all[minimum],'2','.',',').'</td>
		<td>'.number_format($row_all[maximum],'2','.',',').'</td>
		<td>'.number_format($row_all[quantity],'0','.',',').'</td>
		</tr>
		<tr>
		<td>Last Month</td>
		<td>'.number_format(@$row_month[price]/$row_month[quantity],'2','.',',').'</td>
		<td>'.number_format($row_month[minimum],'2','.',',').'</td>
		<td>'.number_format($row_month[maximum],'2','.',',').'</td>
		<td>'.number_format($row_month[quantity],'0','.',',').'</td>
		</tr>
		<tr>
		<td>Last Week</td>
		<td>'.number_format(@$row_week[price]/$row_week[quantity],'2','.',',').'</td>
		<td>'.number_format($row_week[minimum],'2','.',',').'</td>
		<td>'.number_format($row_week[maximum],'2','.',',').'</td>
		<td>'.number_format($row_week[quantity],'0','.',',').'</td>
		</tr>
		</table>
</div>';

$region = array();
$querymonth = "SELECT * FROM `".$settings[mysql_real_escape_string($_GET['wallet']).'transactionstable']."` WHERE `typeID` = '".mysql_real_escape_string($_GET['typeID'])."' AND `transactionDateTime` >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)";
$resultmonth = $db->query($querymonth);
while ($rowmonth = $db->fetch_assoc($resultmonth)) {
array_push($region, get_region($rowmonth['stationID']));
$regionm[get_region($rowmonth['stationID'])]['quantity'] = ($regionm[get_region($rowmonth['stationID'])]['quantity'] + $rowmonth['quantity']);
$regionm[get_region($rowmonth['stationID'])]['price'] = ($regionm[get_region($rowmonth['stationID'])]['price'] + ($rowmonth['price'] * $rowmonth['quantity']));
$regionm[get_region($rowmonth['stationID'])]['lastprice'] = $rowmonth['price'];
}
$queryweek = "SELECT * FROM `".$settings[mysql_real_escape_string($_GET['wallet']).'transactionstable']."` WHERE `typeID` = '".mysql_real_escape_string($_GET['typeID'])."' AND `transactionDateTime` >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)";
$resultweek = $db->query($queryweek);
while ($rowweek = $db->fetch_assoc($resultweek)) {
array_push($region, get_region($rowweek['stationID']));
$regionw[get_region($rowweek['stationID'])]['quantity'] = ($regionw[get_region($rowweek['stationID'])]['quantity'] + $rowweek['quantity']);
$regionw[get_region($rowweek['stationID'])]['price'] = ($regionw[get_region($rowweek['stationID'])]['price'] + ($rowweek['price'] * $rowweek['quantity']));
$regionw[get_region($rowweek['stationID'])]['lastprice'] = $rowweek['price'];
}
$region = array_unique($region);
echo '<div style="clear:right;clear:left;padding-left:5px;">
	<table style="text-align:right;width:400px;">
		<tr>
		<td style="width:75px;">'.$reg.'</td>
		<td>Average</td>
		<td>Volume</td>
		<td>Last Price</td>
		</tr>';
foreach ($region as $reg) {
$regionm[$reg]['average'] = @($regionm[$reg]['price']/$regionm[$reg]['quantity']);
$regionw[$reg]['average'] = @($regionw[$reg]['price']/$regionw[$reg]['quantity']);
echo '
		<tr>
		<td colspan="3" style="text-align:left;">
		'.$reg.'
		</td>
		<tr>
		<td>Last Month</td>
		<td>'.number_format($regionm[$reg]['average'],'2','.',',').'</td>
		<td>'.number_format($regionm[$reg]['quantity'],'0','','').'</td>
		<td>'.number_format($regionm[$reg]['lastprice'],'0','.',',').'</td>
		</tr>
		<tr>
		<td>Last Week</td>
		<td>'.number_format($regionw[$reg]['average'],'2','.',',').'</td>
		<td>'.number_format($regionw[$reg]['quantity'],'0','','').'</td>
		<td>'.number_format($regionw[$reg]['lastprice'],'0','.',',').'</td>
		</tr>
';
}
echo '</table>
</div>';

echo '	</div>
';
} # end typeID if

if (!empty($_GET['wallet']) && empty($_GET['length']) && empty($_GET['typeID'])) {
$queryr = "SELECT COUNT(DISTINCT DATE(`date`)) AS rows FROM `".$settings[mysql_real_escape_string($_GET['wallet']).'journaltable']."`";
$resultr = mysql_query($queryr);
$rowr = mysql_fetch_assoc($resultr);
$limit = $settings['overviewlimit'];
echo pagination_overview($rowr['rows'],$_GET['wallet']);
echo '
<div style="margin:0 auto;width:350px;">
<ul id="butnav" style="padding-left:0px;">
	<li><a href="?wallet=personal&length='.$_GET['length'].'"'.($_GET['wallet'] == 'personal' ? " class=\"active\"" : "").'>Personal</a></li>
	<li><a class="active" href="?wallet='.$_GET['wallet'].'">Daily</a></li>
	<li><a href="?wallet='.$_GET['wallet'].'&length=week">Weekly</a></li>
	<li><a href="?wallet='.$_GET['wallet'].'&length=month">Monthly</a></li>
	<li><a href="?wallet=corp&length='.$_GET['length'].'"'.($_GET['wallet'] == 'corp' ? " class=\"active\"" : "").'>Corporate</a></li>
	</ul>
</div>';
echo '</td></tr></table>';
	for ($n = 0 + $start; $settings['overviewlimit'] + $start >= $n+1; $n++) {
$querydate = "SELECT DISTINCT `date` FROM `".$settings[$_GET['wallet'].'journaltable']."` ORDER BY `date` DESC LIMIT 1";
$resultdate = $db->query($querydate);
$rowdate = $db->fetch_assoc($resultdate);
$rowdate['date'] = explode("-",$rowdate['date']);
$rowdate['date'][2] = explode(" ",$rowdate['date'][2]);
$date  = date("Y-m-d",mktime(0, 0, 0, $rowdate['date'][1], $rowdate['date'][2][0]-$n, $rowdate['date'][0]));
$query = "SELECT * FROM `".$settings[$_GET['wallet'].'journaltable']."` WHERE `date` <= DATE_SUB(DATE_FORMAT( NOW( ),'%Y-%m-%d 23:59:59'), INTERVAL DATEDIFF(NOW(),'".$date."') DAY) AND `date` >= DATE_SUB(DATE_FORMAT( NOW( ),'%Y-%m-%d 00:00:00'), INTERVAL DATEDIFF(NOW(),'".$date."') DAY) ORDER BY `date` DESC";
$result = $db->query($query);
if ($db->foundRows > 0) {
echo '<div style="width:1050px;margin:0 auto;">';
	echo '
	<div style="margin-top:10px;">
	<ul id="tabnav" style="padding-left:0px;">
	<li id="innertab">'.$date.'</li>
	<li id="time">'.get_isk_net($_GET['wallet'],$date).'</li>
	</ul>
	</div>
		<table style="width:100%;border:1px solid orange;border-top:0px;background-color:#fff;">
			<tr><td>
			<ul id="butnav" style="margin-left:2px;padding:0px;">
			<li><a href="#">Transactions</a></li>
			<li><a href="#">Journal</a></li>
			<li><a href="#">Overview</a></li>
			<li id="button">ISK in <font color="green">'.get_isk_in($_GET['wallet'],$date).'</font></li>
			<li id="button">ISK out <font color="red">'.get_isk_out($_GET['wallet'],$date).'</font></li>
			</ul>
			</td></tr>
			<tr>
			<td colspan="5">
			<div id="content'.$n.'">';
$data = array();
while ($row = $db->fetch_assoc($result)) {
if ($row['amount'] > 0) $data[$row['refTypeID']]['in'] = $data[$row['refTypeID']]['in'] + $row['amount'];
if ($row['amount'] < 0) $data[$row['refTypeID']]['out'] = $data[$row['refTypeID']]['out'] + $row['amount'];
$data[$row['refTypeID']]['count']++;
}
echo '<ul style="margin-left:2px;padding:0px;">';
foreach($data as $refID => $array) {
echo '
	<li style="list-style-type:none;margin-bottom:4px;float:left;width:194px;line-height: 14px;padding: 2px 4px 2px 4px;margin-right: 4px;border: 1px solid orange;text-decoration: none;">
		<div style="float:left;">'.get_reference($refID).'</div>
		<div style="float:right;">'.$array['count'].'</div>
		<br />
		<div style="float:left;color:green;text-align:left">'.number_format($array['in'],2,'.',',').'</div>
		<div style="float:right;color:red;text-align:right">'.number_format($array['out'],2,'.',',').'</div>
	</li>';
}
	echo '	</ul>
			</div>
			</td>
			</tr>
		</table>
	';
echo '</div>';
}
	}
echo '<table id="wrapper" border="0" cellpadding="0" cellspacing="0" style="margin-top:10px;border-top:1px solid orange;"><tr><td>';
echo pagination_overview($rowr['rows'],$_GET['wallet']);
echo '</td></tr></table>';
} # end if journal day

if (!empty($_GET['wallet']) == 'personal' && $_GET['length'] == 'week' && empty($_GET['typeID'])) {
$queryr = "SELECT COUNT(DISTINCT WEEK(`date`)) AS rows FROM `".$settings[mysql_real_escape_string($_GET['wallet']).'journaltable']."`";
$resultr = mysql_query($queryr);
$rowr = mysql_fetch_assoc($resultr);
$limit = $settings['overviewlimit'];
echo '
<div style="margin:0 auto;width:350px;">
<ul id="butnav" style="padding-left:0px;">
	<li><a href="?wallet=personal&length='.$_GET['length'].'"'.($_GET['wallet'] == 'personal' ? " class=\"active\"" : "").'>Personal</a></li>
	<li><a href="?wallet='.$_GET['wallet'].'">Daily</a></li>
	<li><a class="active" href="?wallet='.$_GET['wallet'].'&length=week">Weekly</a></li>
	<li><a href="?wallet='.$_GET['wallet'].'&length=month">Monthly</a></li>
	<li><a href="?wallet=corp&length='.$_GET['length'].'"'.($_GET['wallet'] == 'corp' ? " class=\"active\"" : "").'>Corporate</a></li>
	</ul>
</div>';
echo pagination_overview($rowr['rows'],$_GET['wallet']);
echo '</td></tr></table>';
	for ($n = 0 + $start; $settings['overviewlimit'] + $start >= $n+1; $n++) {
$querydate = "SELECT DISTINCT `date` FROM `".$settings[$_GET['wallet'].'journaltable']."` ORDER BY `date` DESC LIMIT 1";
$resultdate = $db->query($querydate);
$rowdate = $db->fetch_assoc($resultdate);
$rowdate['date'] = explode("-",$rowdate['date']);
$rowdate['date'][2] = explode(" ",$rowdate['date'][2]);
$week  = date("W",mktime(0, 0, 0, $rowdate['date'][1], $rowdate['date'][2][0]-($n*7), $rowdate['date'][0]));
$dates = dates(date("Y"), $week); 
$query = "SELECT * FROM `".$settings[$_GET['wallet'].'journaltable']."` WHERE WEEK(`date`,1) = ".$week." ORDER BY `date` DESC";
$result = $db->query($query);
if ($db->foundRows > 0) {
echo '<div style="width:1050px;margin:0 auto;">';
	echo '
	<div style="margin-top:10px;">
	<ul id="tabnav" style="padding-left:0px;">
	<li id="innertab">Week '.$week.'</li>
	<li id="innertab">'.$dates['0'].' to '.$dates['1'].'</li>
	<li id="time">'.get_isk_week_net($_GET['wallet'],$week).'</li>
	</ul>
	</div>
		<table style="width:100%;border:1px solid orange;border-top:0px;background-color:#fff;">
			<tr><td>
			<ul id="butnav" style="margin-left:2px;padding:0px;">
			<li><a href="#">Transactions</a></li>
			<li><a href="#">Journal</a></li>
			<li><a href="#">Overview</a></li>
			<li id="button">ISK in <font color="green">'.get_isk_week_in($_GET['wallet'],$week).'</font></li>
			<li id="button">ISK out <font color="red">'.get_isk_week_out($_GET['wallet'],$week).'</font></li>
			</ul>
			</td></tr>
			<tr>
			<td colspan="5">
			<div id="content'.$n.'">';
$data = array();
while ($row = $db->fetch_assoc($result)) {
if ($row['amount'] > 0) $data[$row['refTypeID']]['in'] = $data[$row['refTypeID']]['in'] + $row['amount'];
if ($row['amount'] < 0) $data[$row['refTypeID']]['out'] = $data[$row['refTypeID']]['out'] + $row['amount'];
$data[$row['refTypeID']]['count']++;
}
echo '<ul style="margin-left:2px;padding:0px;">';
foreach($data as $refID => $array) {
echo '
	<li style="list-style-type:none;margin-bottom:4px;float:left;width:194px;line-height: 14px;padding: 2px 4px 2px 4px;margin-right: 4px;border: 1px solid orange;text-decoration: none;">
		<div style="float:left;">'.get_reference($refID).'</div>
		<div style="float:right;">'.$array['count'].'</div>
		<br />
		<div style="float:left;color:green;text-align:left">'.number_format($array['in'],2,'.',',').'</div>
		<div style="float:right;color:red;text-align:right">'.number_format($array['out'],2,'.',',').'</div>
	</li>';
}
	echo '	</ul>
			</div>
			</td>
			</tr>
		</table>
	';
echo '</div>';
}
	}
echo '<table id="wrapper" border="0" cellpadding="0" cellspacing="0" style="margin-top:10px;border-top:1px solid orange;"><tr><td>';
echo pagination_overview($rowr['rows'],$_GET['wallet']);
echo '</td></tr></table>';
} # end if journal week

if (!empty($_GET['wallet']) && $_GET['length'] == 'month' && empty($_GET['typeID'])) {
$queryr = "SELECT COUNT(DISTINCT MONTH(`date`)) AS rows FROM `".$settings[mysql_real_escape_string($_GET['wallet']).'journaltable']."`";
$resultr = mysql_query($queryr);
$rowr = mysql_fetch_assoc($resultr);
$limit = $settings['overviewlimit'];
echo '
<div style="margin:0 auto;width:350px;">
<ul id="butnav" style="padding-left:0px;">
	<li><a href="?wallet=personal&length='.$_GET['length'].'"'.($_GET['wallet'] == 'personal' ? " class=\"active\"" : "").'>Personal</a></li>
	<li><a href="?wallet='.$_GET['wallet'].'">Daily</a></li>
	<li><a href="?wallet='.$_GET['wallet'].'&length=week">Weekly</a></li>
	<li><a class="active" href="?wallet='.$_GET['wallet'].'&length=month">Monthly</a></li>
	<li><a href="?wallet=corp&length='.$_GET['length'].'"'.($_GET['wallet'] == 'corp' ? " class=\"active\"" : "").'>Corporate</a></li>
	</ul>
</div>';
echo pagination_overview($rowr['rows'],$_GET['wallet']);
echo '</td></tr></table>';
	for ($n = 0 + $start; $settings['overviewlimit'] + $start >= $n+1; $n++) {
$querydate = "SELECT DISTINCT `date` FROM `".$settings[$_GET['wallet'].'journaltable']."` ORDER BY `date` DESC LIMIT 1";
$resultdate = $db->query($querydate);
$rowdate = $db->fetch_assoc($resultdate);
$rowdate['date'] = explode("-",$rowdate['date']);
$rowdate['date'][2] = explode(" ",$rowdate['date'][2]);
$month  = date("F",mktime(0, 0, 0, $rowdate['date'][1]-$n, $rowdate['date'][2][0], $rowdate['date'][0]));
$query = "SELECT * FROM `".$settings[$_GET['wallet'].'journaltable']."` WHERE MONTHNAME(`date`) = '".$month."' ORDER BY `date` DESC";
$result = $db->query($query);
if ($db->foundRows > 0) {
echo '<div style="width:1050px;margin:0 auto;">';
	echo '
	<div style="margin-top:10px;">
	<ul id="tabnav" style="padding-left:0px;">
	<li id="innertab">'.$month.'</li>
	<li id="time">'.get_isk_month_net($_GET['wallet'],$month).'</li>
	</ul>
	</div>
		<table style="width:100%;border:1px solid orange;border-top:0px;background-color:#fff;">
			<tr><td>
			<ul id="butnav" style="margin-left:2px;padding:0px;">
			<li><a href="#">Transactions</a></li>
			<li><a href="#">Journal</a></li>
			<li><a href="#">Overview</a></li>
			<li id="button">ISK in <font color="green">'.get_isk_month_in($_GET['wallet'],$month).'</font></li>
			<li id="button">ISK out <font color="red">'.get_isk_month_out($_GET['wallet'],$month).'</font></li>
			</ul>
			</td></tr>
			<tr>
			<td colspan="5">
			<div id="content'.$n.'">';
$data = array();
while ($row = $db->fetch_assoc($result)) {
if ($row['amount'] > 0) $data[$row['refTypeID']]['in'] = $data[$row['refTypeID']]['in'] + $row['amount'];
if ($row['amount'] < 0) $data[$row['refTypeID']]['out'] = $data[$row['refTypeID']]['out'] + $row['amount'];
$data[$row['refTypeID']]['count']++;
}
echo '<ul style="margin-left:2px;padding:0px;">';
foreach($data as $refID => $array) {
echo '
	<li style="list-style-type:none;margin-bottom:4px;float:left;width:194px;line-height: 14px;padding: 2px 4px 2px 4px;margin-right: 4px;border: 1px solid orange;text-decoration: none;">
		<div style="float:left;">'.get_reference($refID).'</div>
		<div style="float:right;">'.$array['count'].'</div>
		<br />
		<div style="float:left;color:green;text-align:left">'.number_format($array['in'],2,'.',',').'</div>
		<div style="float:right;color:red;text-align:right">'.number_format($array['out'],2,'.',',').'</div>
	</li>';
}
	echo '	</ul>
			</div>
			</td>
			</tr>
		</table>
	';
echo '</div>';
}
	}
echo '<table id="wrapper" border="0" cellpadding="0" cellspacing="0" style="margin-top:10px;border-top:1px solid orange;"><tr><td>';
echo pagination_overview($rowr['rows'],$_GET['wallet']);
echo '</td></tr></table>';
} # end if journal month

if (!empty($_GET['typeID'])) {
echo '
<div style="float:right;width:602px;margin-top:10px;">
<ul id="tabnav" style="padding-left:150px;">
	<li><a href="javascript:generateimage(\'itemgraph.php?typeID='.intval($_GET['typeID']).'&wallet='.mysql_real_escape_string($_GET['wallet']).'\');" href="#">Last 50 Orders</a></li>
	<li><a href="javascript:generateimage(\'itemgraph.php?typeID='.intval($_GET['typeID']).'&wallet='.mysql_real_escape_string($_GET['wallet']).'&graph=week\');">Last Week</a></li>
	<li><a href="javascript:generateimage(\'itemgraph.php?typeID='.intval($_GET['typeID']).'&wallet='.mysql_real_escape_string($_GET['wallet']).'&graph=month\');">Last Month
</a></li>
</ul>
<div id="graphs" style="border:1px solid orange;border-top:0px;width:600px;height:400px;"><img src="itemgraph.php?typeID='.intval($_GET['typeID']).'&wallet='.mysql_real_escape_string($_GET['wallet']).'&graph='.mysql_real_escape_string($_GET['graph']).'" border="0" /></div>
</div>';
} if (empty($_GET['wallet'])) {
echo '

<div style="margin:0 auto;width:190px;">
<ul id="butnav" style="padding-left:0px;">
	<li><a href="?wallet=personal">Personal</a></li>
	<li><a href="?wallet=corp">Corporate</a></li>
	</ul>
</div>
<div style="float:right;width:602px;margin-top:10px;">
<ul id="tabnav" style="padding-left:150px;">
	<li><a href="javascript:generateimage(\'itemgraph.php?wallet=personal&graph=balance\');">Personal Wallet</a></li>
	<li><a href="javascript:generateimage(\'itemgraph.php?wallet=corp&graph=balance\');">Corporate Wallet</a></li>
</ul>
<div id="graphs" style="border:1px solid orange;border-top:0px;width:600px;height:400px;"><img src="itemgraph.php?wallet=personal&graph=balance" border="0" /></div>
</div>';
}

echo '
		</td>
	</tr>
</table>
';


include("footer.php");
?>